package com.utils
{
	/**
	 * 提供对目标说明解释 
	 * 使用之前先注册舞台和父容器
	 */	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.text.StaticText;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	public class ITip extends Sprite
	{
		private static var _stage:Stage;
		private static var _parentContain:DisplayObjectContainer;
		private var contain:Sprite;
		private var bgContain:Sprite;
		private var infoContain:Sprite;
		private var txtInfoArr:Array = [];
		private var txtArr:Array = [];
		private var _info:Object;
		private var _resoucePool:Object = new Object();
		private var _target:DisplayObject;
		
		private static var _iTip:ITip;
		public function ITip()
		{
			if(!_stage || !_parentContain){
				throw Error('ITip-->调用register()注册舞台和父容器!');
			}
			
			if(_iTip){
				throw Error('ITip不能重复创建实例!')
			}else{
				_iTip = this;
				_parentContain.addChild(this);
			}
			
			this.mouseEnabled = false;
			this.mouseChildren = false;
			trace('--->创建ITip实例!')
		}
		
		/**
		 * 注册舞台和父容器 
		 * @param stage
		 * @param parentContain
		 */		
		public static function register(root:DisplayObjectContainer,parentContain:DisplayObjectContainer):void
		{
			_stage = root.stage;
			_parentContain = parentContain;
		}
		
		public static function instance():ITip{
			if(!_iTip) _iTip = new ITip();
			return _iTip;
		}
		
		public function addTarget(target:DisplayObject,info:* = null):void{
			if(!contain){
				contain = new Sprite();
				this.addChild(contain);
			}
			
			if(!bgContain){
				bgContain = new Sprite();
				contain.addChild(bgContain);
			} 
			
			if(!infoContain){
				infoContain = new Sprite();
				contain.addChild(infoContain);
			}
			
			_target = target;
			_resoucePool[_target.name] = info;
			
			if(!hasEventListener(Event.ENTER_FRAME)){
				this.addEventListener(Event.ENTER_FRAME,onLoop);
			}
			
			if(!_target.hasEventListener(MouseEvent.MOUSE_OVER)){
				_target.addEventListener(MouseEvent.MOUSE_OVER,onMouseMoveHandler);
				_target.addEventListener(MouseEvent.MOUSE_OUT,onMouseMoveHandler);
			}
		}
		
		private function onLoop(event:Event):void
		{
			if(mouseX + contain.width > _stage.stageWidth){
				contain.x = mouseX - contain.width;
			}else{
				contain.x = mouseX + 10;
			}
			if(mouseY + contain.height > _stage.stageHeight){
				contain.y = mouseY - contain.height;
			}else{
				contain.y = mouseY + 10;
			}
		}		
		
		private function onMouseMoveHandler(e:MouseEvent):void
		{
			if(e.type == MouseEvent.MOUSE_OVER){
				
				_info = _resoucePool[e.currentTarget.name];
				if(!_info) return;
				if(_info is Sprite || _info is MovieClip){
					infoContain.addChild(_info as DisplayObjectContainer);
					addBgContain(-1,-1,1,1,0,0,0);
				}else{
					addObjectTxt();
					addBgContain();
				}
				
				
			}else if(e.type == MouseEvent.MOUSE_OUT){
				
				if(_info is Sprite || _info is MovieClip){
					
					while(infoContain.numChildren) infoContain.removeChildAt(0);
					
				}else{
					
					for(var i:int = 0; i < txtArr.length; i++) 
						(txtArr[i] as TextField).parent.removeChild(txtArr[i]);
					
					txtArr = [];
				}
				
				bgContain.graphics.clear();
			}
		}
		
		/**
		 * 添加背景
		 * @param offsetX
		 * @param offsetY
		 */		
		private function addBgContain(x:int = 0,y:int = 0,
									  offsetX:int = 20,offsetY:int = 10,
									  color:int = 0xa39271,roundW:int = 10,roundH:int = 10):void{
			bgContain.graphics.clear();
			bgContain.graphics.lineStyle(1,color);
			bgContain.graphics.beginFill(0,.8);
			bgContain.graphics.drawRoundRect(x,y,infoContain.width+offsetX,infoContain.height + offsetY,roundW,roundH);
			bgContain.graphics.endFill();
		}
		
		/**添加文本*/		
		private function addObjectTxt():void{
			txtArr = []; 
			txtInfoArr = [];
			
			//String继承Object 先判断String 在判断Object
			if(_info is String){
				txtInfoArr = RegExpUtil.stringWrap(_info);
			}else if(_info is Object){
				for(var key:String in _info) txtInfoArr.push(String(_info[key]));
			}
			
			for (var i:int = 0; i < txtInfoArr.length; i++) 
			{
				var txt:TextField = new TextField();
				txt.height = 30;
				txt.htmlText = '<font color="#FFFFFF">' +　txtInfoArr[i] + '</font>';
				txt.autoSize = TextFieldAutoSize.LEFT;
				txt.x = 10;
				txt.y += txt.textHeight*i + 5;
				infoContain.addChild(txt);
				txtArr.push(txt);
			}
			txtInfoArr = [];
		}
		
		/**
		 * 移除提示
		 * @param target 目标
		 * @param removeThisAll 是否移除目标内所有元件的提示
		 */		
		public function removeITips(target:DisplayObjectContainer,removeThisAll:Boolean = false):void{
			if(removeThisAll){
				for(var index:int=0;index < target.numChildren;index++){
					var childrenName:String = target.getChildAt(index).name;
					delete _resoucePool[childrenName];
				}
			}
			delete _resoucePool[target.name];
		}
		
		
		
		
		
	}
}